package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FileMapper;
import com.itheima.dao.UserMapper;
import com.itheima.pojo.File;
import com.itheima.pojo.User;
import com.itheima.service.UserService;

import com.itheima.utils.IdWorker;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private FileMapper fileMapper;


    /**
     * 根据uid删除用户
     *
     * @param user
     */

    @Override
    public void delete(User user) {
        userMapper.delete(user);

    }

    /**
     * 编辑User
     *
     * @param user
     */
    @Override
    public void edit(User user) {

        User user1 = userMapper.selectByPrimaryKey(user.getUid());
        user.setCreateTime(user1.getCreateTime());
        user.setUpdateTime(new Date());
        userMapper.delete(user1);
        userMapper.insert(user);

    }

    /**
     * 条件+分页查询
     *
     * @param user
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<User> getList(User user, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(user);
        return (Page<User>) userMapper.selectByExample(example);
    }

    /**
     * 重置用户密码后为1234
     *
     * @param user
     */
    @Override
    public void resetUserPassword(User user) {
        String passWord = MD5Utils.md5("1234");
        String uid = user.getUid();
        userMapper.resetUserPassword(uid, passWord);
    }

    /**
     * 根据用户名查用户
     *
     * @param username
     */
    @Override
    public User findByUsername(String username) {
        User user = new User();
        user.setUserName(username);
        return userMapper.selectOne(user);
    }

    /**
     * 注册
     *
     * @param user
     */
    @Override
    public void register(User user) {
        User user1 = new User();
        user1.setUserName(user.getUserName());
        User user2 = userMapper.selectOne(user1);
        if (user2 != null) {
            throw new RuntimeException("该用户名已存在");
        }

        String passWord = user.getPassWord();
        user.setPassWord(MD5Utils.md5(passWord));

        user.setUid(String.valueOf(idWorker.nextId()));
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insertSelective(user);
    }

    /**
     * 编辑用户信息
     *
     * @Param user
     */
    @Override
    @Transactional
    public Integer editUser(User user) {
        User user1 = new User();
        user1.setUserName(user.getUserName());
        user1.setUid(user.getUid());

        user.setUpdateTime(new Date());

        if (user.getAvatar() != null) {
            return userMapper.updateByExampleSelective(user, createExample(user1));
        }

        if (user.getPhotoUrl() != null) {
            File file = new File();
            file.setPicUrl(user.getPhotoUrl());
            String uid = String.valueOf(idWorker.nextId());
            file.setUid(uid);
            file.setCreateTime(new Date());
            file.setUpdateTime(new Date());
            fileMapper.insertSelective(file);
            user.setAvatar(uid);
        }

        return userMapper.updateByExampleSelective(user, createExample(user1));
    }

    @Override
    public List<User> findAll() {
      return  userMapper.selectAll();
    }

    /**
     * 构建查询对象
     *
     * @param user
     * @return
     */
    private Example createExample(User user) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (user != null) {
            // uid
            if (user.getUid() != null && !"".equals(user.getUid())) {
                criteria.andEqualTo("uid", user.getUid());
            }
            // 用户名
            if (user.getUserName() != null && !"".equals(user.getUserName())) {
                criteria.andLike("userName","%" + user.getUserName()+ "%");
            }
            // 性别，1男，0女
            if (user.getGender() != null && !"".equals(user.getGender())) {
                criteria.andEqualTo("gender", user.getGender());
            }
            // 注册邮箱
            if (user.getEmail() != null && !"".equals(user.getEmail())) {
                criteria.andLike("email", "%" + user.getEmail() + "%");
            }
            // 手机号
            if (user.getMobile() != null && !"".equals(user.getMobile())) {
                criteria.andEqualTo("mobile", user.getMobile());
            }
            // 最后登录IP
            if (user.getLastLoginIp() != null && !"".equals(user.getLastLoginIp())) {
                criteria.andEqualTo("lastLoginIp", user.getLastLoginIp());
            }
            // 使用状态（1正常 0非正常）
            if (user.getStatus() != null && !"".equals(user.getStatus())) {
                criteria.andEqualTo("status", user.getStatus());
            }
            // 资料来源
            if (user.getSource() != null && !"".equals(user.getSource())) {
                criteria.andEqualTo("source", user.getSource());
            }
            // 昵称
            if (user.getNickName() != null && !"".equals(user.getNickName())) {
                criteria.andLike("nickName", "%" + user.getNickName() + "%");
            }
            // QQ号码
            if (user.getQqNumber() != null && !"".equals(user.getQqNumber())) {
                criteria.andLike("qqNumber", "%" + user.getQqNumber() + "%");
            }
            // 微信号码
            if (user.getWeChat() != null && !"".equals(user.getWeChat())) {
                criteria.andLike("weChat", "%" + user.getWeChat() + "%");
            }

            // 职业
            if (user.getOccupation() != null && !"".equals(user.getOccupation())) {
                criteria.andEqualTo("occupation", user.getOccupation());
            }
            // 评论状态
            if (user.getCommentStatus() != null && !"".equals(user.getCommentStatus())) {
                criteria.andEqualTo("commentStatus", user.getCommentStatus());
            }
            //IP来源
            if (user.getIpSource() != null && !"".equals(user.getIpSource())) {
                criteria.andEqualTo("ipSource", user.getIpSource());
            }

            //用户标签
            if (user.getUserTag() != null && !"".equals(user.getUserTag())) {
                criteria.andEqualTo("userTag", user.getUserTag());
            }
        }
        return example;
    }
}
